﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraPrinting;
using DevExpress.XtraPrinting.Native;
using DevExpress.XtraReports.UI;
using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.EventBroker;
using NetRosApp.Infrastructure.Interface.Constants;

namespace NetRosApp.Infrastructure.Interface.Base
{
    public class BaseAdvOtvaranjeIzvjestaja
    {
        
             private PageSettings _last = null;
        protected void OpenReportAdv(string _naziv, XtraReport _report, WorkItem workitem, int tipispisa)
        {


            if (_report != null && tipispisa == 1)
            {

                //   _report.CreateDocument();

                PrinterSettings settings = new PrinterSettings();
                //old
                string printertoprint = BasePrinterUtility.GetPrinterToPrinter();
                settings.PrinterName = printertoprint;
                _report.PrinterName = printertoprint;
                //_report.PrintingSystem.PageSettings.PrinterName = printertoprint;
                _report.PrintingSystem.StartPrint += PrintingSystem_StartPrint;

                _report.PrintingSystem.ShowMarginsWarning = false;
                _report.PaperKind = settings.DefaultPageSettings.PaperSize.Kind;
                _report.PaperName = settings.DefaultPageSettings.PaperSize.PaperName;

                //_report.PageSize
                //_report.PageSize.Height = settings.DefaultPageSettings.PaperSize.Height;
                //_report.PageSize.Width = settings.DefaultPageSettings.PaperSize.Width;
                _report.PrintingSystem.PageSettings.PaperKind = settings.DefaultPageSettings.PaperSize.Kind;
                _report.PrintingSystem.PageSettings.PaperName = settings.DefaultPageSettings.PaperSize.PaperName;
                PageSettings _pageSettings = new PageSettings(settings);
                bool landscape = _report.Landscape;
                       _pageSettings.Landscape = _report.Landscape;
                 Margins _minmarg = new Margins(0,0,0,0);
                        if (settings.DefaultPageSettings.Landscape)
                        {
                            _minmarg = GetMinMarginsMojLandS(_pageSettings);
                            
                        }else
                        {
                           _minmarg = GetMinMarginsMoj(_pageSettings);
                        }
                        _pageSettings.Margins = _minmarg;
                        _report.PrintingSystem.PageSettings.Assign(_pageSettings,new Margins(0,0,0,0));
                        _report.PrintingSystem.PageSettings.Landscape = landscape;
                        _report.Margins = _pageSettings.Margins;
/*
                if (_report.Landscape)
                {
                       if (settings.LandscapeAngle == 90)
                            {
                                _pageSettings.Margins.Top = (int)Math.Round(settings.DefaultPageSettings.PrintableArea.Left,MidpointRounding.AwayFromZero)+1;
                                _pageSettings.Margins.Left = (int)Math.Round(settings.DefaultPageSettings.PaperSize.Height - settings.DefaultPageSettings.PrintableArea.Height - settings.DefaultPageSettings.PrintableArea.Top,MidpointRounding.AwayFromZero)+1;
                                _pageSettings.Margins.Bottom  = (int)Math.Round(settings.DefaultPageSettings.PaperSize.Width - settings.DefaultPageSettings.PrintableArea.Width - settings.DefaultPageSettings.PrintableArea.Left,MidpointRounding.AwayFromZero)+1;    
                                _pageSettings.Margins.Right = (int)Math.Round(settings.DefaultPageSettings.PrintableArea.Top,MidpointRounding.AwayFromZero)+1;
 
                            }
                            if (settings.LandscapeAngle == 270)
                            {
                                _pageSettings.Margins.Top = (int)Math.Round(settings.DefaultPageSettings.PaperSize.Width - settings.DefaultPageSettings.PrintableArea.Width - settings.DefaultPageSettings.PrintableArea.Left,MidpointRounding.AwayFromZero)+1;    
                                _pageSettings.Margins.Left = (int)Math.Round(settings.DefaultPageSettings.PrintableArea.Top,MidpointRounding.AwayFromZero)+1;
                                _pageSettings.Margins.Bottom = (int)Math.Round(settings.DefaultPageSettings.PrintableArea.Left,MidpointRounding.AwayFromZero)+1;
                                _pageSettings.Margins.Right = (int)Math.Round(settings.DefaultPageSettings.PaperSize.Height - settings.DefaultPageSettings.PrintableArea.Height - settings.DefaultPageSettings.PrintableArea.Top,MidpointRounding.AwayFromZero)+1;
                            }

                }
                else
                {
                    //_pageSettings.Margins.Top = (int)Math.Round(settings.DefaultPageSettings.PrintableArea.Top);
                    //_pageSettings.Margins.Left =
                    //    (int)Math.Round(settings.DefaultPageSettings.PrintableArea.Left);
                    //_pageSettings.Margins.Bottom =
                    //    (int)
                    //    Math.Round(settings.DefaultPageSettings.PaperSize.Height -
                    //               settings.DefaultPageSettings.PrintableArea.Height -
                    //               settings.DefaultPageSettings.PrintableArea.Top);
                    //_pageSettings.Margins.Right =
                    //    (int)
                    //    Math.Round(settings.DefaultPageSettings.PaperSize.Width -
                    //               settings.DefaultPageSettings.PrintableArea.Width -
                    //               settings.DefaultPageSettings.PrintableArea.Left);
                      _pageSettings.Margins.Top = (int)Math.Round(settings.DefaultPageSettings.PrintableArea.Top,MidpointRounding.AwayFromZero)+1;
                            _pageSettings.Margins.Left = (int)Math.Round(settings.DefaultPageSettings.PrintableArea.Left,MidpointRounding.AwayFromZero)+1;
                            _pageSettings.Margins.Bottom = (int)Math.Round(settings.DefaultPageSettings.PaperSize.Height - settings.DefaultPageSettings.PrintableArea.Height - settings.DefaultPageSettings.PrintableArea.Top,MidpointRounding.AwayFromZero)+1;
                            _pageSettings.Margins.Right = (int)Math.Round(settings.DefaultPageSettings.PaperSize.Width - settings.DefaultPageSettings.PrintableArea.Width - settings.DefaultPageSettings.PrintableArea.Left,MidpointRounding.AwayFromZero)+1;   
                }
              
                _report.PrintingSystem.PageSettings.Assign(_pageSettings);
                _report.PrintingSystem.PageSettings.Landscape = landscape;
                _report.Margins = _pageSettings.Margins;
 * */
                  _last = _pageSettings;
                _report.CreateDocument();
                //((PrintingSystem)_report.PrintingSystem).Print(printertoprint);
                _report.Print(printertoprint);
            }
            if (tipispisa == 0)
                workitem.EventTopics[EventTopicNames.PrikaziIzvjestaj].Fire("BaseAdvIzvjestaji",
                                                                            new EventArgs<object, string>(
                                                                                _report, String.Format(_naziv)),
                                                                            workitem, PublicationScope.Global);

        }
        void PrintingSystem_StartPrint(object sender, DevExpress.XtraPrinting.PrintDocumentEventArgs e)
        {
            e.PrintDocument.PrinterSettings.Copies = Convert.ToInt16(BasePrinterUtility.BrojKopija);
        }
        internal static Margins GetMinMarginsMoj(PageSettings pageSettings) {
			PrinterSettings printerSettings = pageSettings.PrinterSettings;
			if(!String.IsNullOrEmpty(printerSettings.PrinterName)) {
				IntPtr hdc = Win32.CreateDC(null, printerSettings.PrinterName, IntPtr.Zero, IntPtr.Zero);
				if(hdc != IntPtr.Zero)
					try {
						using(Graphics gr = Graphics.FromHdc(hdc)) {
							Margins minMargins = DeviceCaps.GetMinMargins(gr);
							if (pageSettings.Landscape && pageSettings.PrinterSettings.LandscapeAngle == 90)
							    return (ConvertToLandscape90(minMargins));
                            if (pageSettings.Landscape && pageSettings.PrinterSettings.LandscapeAngle == 270)
							    return (ConvertToLandscape270(minMargins));
                            return minMargins;
						}
					} finally {
						Win32.DeleteDC(hdc);
					}
			}
			return null;
		}
        internal static Margins GetMinMarginsMojLandS(PageSettings pageSettings) {
			PrinterSettings printerSettings = pageSettings.PrinterSettings;
			if(!String.IsNullOrEmpty(printerSettings.PrinterName)) {
				IntPtr hdc = Win32.CreateDC(null, printerSettings.PrinterName, IntPtr.Zero, IntPtr.Zero);
				if(hdc != IntPtr.Zero)
					try {
						using(Graphics gr = Graphics.FromHdc(hdc)) {
							Margins minMargins = DeviceCaps.GetMinMargins(gr);
							if (!pageSettings.Landscape && pageSettings.PrinterSettings.LandscapeAngle == 90)
							    return (ConvertToLandscape90(minMargins));
                            if (!pageSettings.Landscape && pageSettings.PrinterSettings.LandscapeAngle == 270)
							    return (ConvertToLandscape270(minMargins));
                            return minMargins;
						}
					} finally {
						Win32.DeleteDC(hdc);
					}
			}
			return null;
		}
        static Margins ConvertToLandscape90(Margins margins) {
			return new Margins(margins.Bottom,margins.Top,margins.Right,margins.Left);
		}
        static Margins ConvertToLandscape270(Margins margins) {
			return new Margins(margins.Top, margins.Bottom, margins.Right, margins.Left);
		}
    }

    public class IzjestajiListe
    {
        private string _Ispis;
        private string _TipIspisa;
        private string _NazivIzvjestaja;
        private string _File;
        public IzjestajiListe(string Ispis, string TipIspisa, string NazivIzvjestaja, string File)
        {
            _Ispis = Ispis;
            _TipIspisa = TipIspisa;
            _NazivIzvjestaja = NazivIzvjestaja;
            _File = File;
        }
        public string TipIspia
        {
            get { return _TipIspisa; }
        }
        public string Ispis
        {
            get { return _Ispis; }
        }

        public string NazivIzvjestaja
        {
            get { return _NazivIzvjestaja; }
        }

        public string File
        {
            get { return _File; }
        }



        public override string ToString()
        {
            return _Ispis;
        }
    }

    public static class DodavanjeIzvjestaja
    {
        public static void DodajIzvjestajeuListu(List<IzjestajiListe> lista, string ImeDirektorijaIliIzvjestaja)
        {
            String lokacija = Application.StartupPath + "\\Izvjestaji" + "\\" + ImeDirektorijaIliIzvjestaja;
            if (Directory.Exists(lokacija))
            {
                DirectoryInfo di = new DirectoryInfo(lokacija);

                DirectoryInfo[] rgDirec = di.GetDirectories();
                foreach (DirectoryInfo directoryInfo in rgDirec)
                {
                    FileInfo[] rgFiles = directoryInfo.GetFiles("*.repx");
                    foreach (FileInfo fi in rgFiles)
                    {
                        fi.ToString();
                        lista.Add(new IzjestajiListe(fi.Name.Replace(".repx", ""), "Lokalno", directoryInfo.Name,
                                                     fi.FullName));
                    }
                }
            }


        }
        public static void DodajIzvjestajDefault(List<IzjestajiListe> lista, string Naziv)
        {
            lista.Add(new IzjestajiListe(Naziv, "", "", ""));
        }

    }
}
